package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.dialog.CookProgressDialog;
import com.lanou.dllo.findfood.entity.PageJson;
import com.lanou.dllo.findfood.entity.PageResult;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 15/10/22.
 */
public class HomeLastActivity extends Activity implements View.OnClickListener {
    private TextView title_name,food_Name,address,level_title,Comment,user_Name,phone;
    private ImageView picture_Url,user_img,btn_back;
    private RequestQueue queue;

    private  long tweet_id;
    private  String place_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        initView();
        getTitltData();
        initData();


    }

    //得到传递的数据值 在这个页面接收得到的值
   private void getTitltData(){
       place_name = getIntent().getStringExtra("place_name");
       title_name.setText(place_name);
       tweet_id = getIntent().getLongExtra("tweet_id",925843);
    }

 public void    initView(){
     title_name = (TextView) findViewById(R.id.title_name);
     food_Name = (TextView) findViewById(R.id.food_Name);
     address = (TextView) findViewById(R.id.address);
     level_title = (TextView) findViewById(R.id.level_title);

     user_img = (ImageView) findViewById(R.id.user_img);
     user_Name = (TextView) findViewById(R.id.user_Name);
     phone = (TextView) findViewById(R.id.phone);
     picture_Url = (ImageView) findViewById(R.id.picture_Url);
     queue = Volley.newRequestQueue(this);
     user_img.setOnClickListener(this);
     btn_back = (ImageView) findViewById(R.id.btn_back);
     btn_back.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             finish();
         }
     });
 }
  public void   initData(){
      final CookProgressDialog anmition = new CookProgressDialog(this, R.id.animaImg);
      anmition.show();
      String url = "http://api.meishixing.com/tweet/tweet/detail/" +
              "session_id=00001227993a5661b25da19c79c2a7c9a6affc&tweet_id=" + tweet_id;
      StringRequest request = new StringRequest(url, new Response.Listener<String>() {
          @Override
          public void onResponse(String response) {
              Gson gson = new Gson();
              PageJson p = gson.fromJson(response,PageJson.class);
              if (p != null ){
                  PageResult result = p.getResult();
                  if (result != null ){
                      title_name.setText(result.getPlace_name());
                      food_Name.setText(result.getFood_name());
                      address.setText(result.getAddress());
                      user_Name.setText(result.getUser_name());
                      String s = result.getPhone();//没有电话号码默认添加
                      if (s != null && s.length() > 0){
                          phone.setText(result.getPhone());
                      }else{
                        String phonenumber = "0411-82714555 82737136";
                          phone.setText(phonenumber);
                      }

                      String user_imgUrl = result.getUser_image();
                      if (user_imgUrl != null && user_imgUrl.length() > 0){
                          getUser_img(user_imgUrl,user_img);
                      }
                      String pictureUrl = result.getPicture_url();
                      if (pictureUrl != null && pictureUrl.length() >0){
                         getUser_img(pictureUrl,picture_Url);


                      }
                      anmition.dismiss();

                  }
              }



          }
      }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
              error.printStackTrace();
          }
      }){
          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
              Map<String, String> map = new HashMap<String, String>();
              map.put("Accept-Encoding", "");
              return map;
          }
      };
      queue.add(request);
    }

   public void getUser_img(String user_img, final ImageView imageView){
       ImageRequest request = new ImageRequest(user_img,
               new Response.Listener<Bitmap>() {
                   @Override
                   public void onResponse(Bitmap response) {
                       imageView.setImageBitmap(response);


                   }
               }, 300, 300, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
               error.printStackTrace();
           }
       }
       ){
           @Override
           public Map<String, String> getHeaders() throws AuthFailureError {
               Map<String, String> map = new HashMap<String, String>();
               map.put("Accept-Encoding", "");
               return map;
           }
       };
       queue.add(request);


   }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.user_img:



            break;
            default:
                break;


        }
    }
}
